﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using G28BS.Models;
using Newtonsoft.Json;

namespace G28BS.Hubs
{
    [Authorize]
    public class TicTacToeHub : MasterHub
    {
        // Transmits the data to the other player(s)
        public void ClickCell(string groupID, string cell)
        {
            // Calls the cellClicked method to update clients in group.
            Clients.OthersInGroup(groupID).cellClicked(cell);
        }

        public void ClickRematch(string groupID)
        {
            Clients.OthersInGroup(groupID).rematchClicked();
        }

        public void FindFirst(string groupID, string number)
        {
            Clients.OthersInGroup(groupID).recieveFirst(number);
        }


        public void LogWin(string p1ID, string p2ID, string winnerID)
        {
            int player1ID = Convert.ToInt32(p1ID);
            int player2ID = Convert.ToInt32(p2ID);

            // TODO: the following code currently hangs somewhere (also does not update any db records)
            if (p1ID == winnerID)
            {
                int idOfWinner = Convert.ToInt32(p1ID);
                int idOfLoser = Convert.ToInt32(p2ID);
                tttResult win = new tttResult(idOfWinner, idOfLoser, false);
            }
            else if (p2ID == winnerID)
            {
                int idOfWinner = Convert.ToInt32(p2ID);
                int idOfLoser = Convert.ToInt32(p1ID);
                tttResult win = new tttResult(idOfWinner, idOfLoser, false);
            }
        }

        public void LogDraw(string p1ID, string p2ID)
        {
            int player1ID = Convert.ToInt32(p1ID);
            int player2ID = Convert.ToInt32(p2ID);

            tttResult draw = new tttResult(player1ID, player2ID, true);
        }
    }
}
    
